/**
 * author: Hanflyee  flyee1980@163.com
 * vision: v1.0
 * data: 2009-7-9
 */
package com.flyee.viewer{
	
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.DisplayObjectUtil;
	import com.flyee.utils.GenerateIDTool;
	import com.flyee.utils.PlatformDepot;
	import com.flyee.utils.TextFilterUtil;
	
	import flash.display.*;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;	
	
	/**
	 * 模拟人物UI
	 */
	public class CharactorViewer extends Sprite implements Destroy{
		
		/**UI元素的ID，必须生成*/
		public var vid : String = GenerateIDTool.generateID();

		/**
		 * PNG的人物动作图板数组，按动作、方向和帧保存
		 */
		public var plateArray : Array = new Array();
		/**
		 * 武器动画 
		 */
		public var weaponCartoon : MovieClip;
		
		public var charactorContainer : Sprite = new Sprite();
		
		public var specialContainer : Sprite = new Sprite();
		
		public var weaponContainer : Sprite = new Sprite();
		
		public var effectContainer : Sprite = new Sprite();
		
		public var nameText : TextField = new TextField();
		
		public var hpShape : Shape = new Shape();
		
		public var playerVisible : Boolean = ObjectCollection.playerVisible;
		
		public function CharactorViewer(){
			addChild(charactorContainer);	
			addChild(weaponContainer);	
			addChild(specialContainer);	
			weaponContainer.visible = false;
			specialContainer.visible = false;
			effectContainer.visible = true;
			addChild(nameText);
			addChild(hpShape);
			drawHPBar(hpShape.graphics);
//			nameText.x = 0;
			effectContainer.x = 12;
			effectContainer.y = -80;
			effectContainer.scaleX = 0.7;
			effectContainer.scaleY = 0.7;
			nameText.y = -90;
			hpShape.y = -105;
			charactorContainer.x = 20;
			charactorContainer.y = -75;
			charactorContainer.scaleX = 0.5;
			charactorContainer.scaleY = 0.5;
			specialContainer.scaleX = 0.5;
			specialContainer.scaleY = 0.5;
			weaponContainer.scaleX = 0.5;
			weaponContainer.scaleY = 0.5;
			addChild(effectContainer);
		}
		
		private function drawHPBar(graph:Graphics,width:int=100):void{
			var colors:Array = null;
			var alphs:Array = [1,1];
			var ratios:Array = [55, 255]; 
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(width,10,Math.PI/2,0,0);
			colors = [0xff6944,0xb90000];
			graph.clear();
			graph.lineStyle(2,0x02adff);
			graph.beginFill(0xffccff,0.4);
			graph.drawRoundRectComplex(0,0,100,10,3,3,3,3);
			graph.lineStyle(2,0x02adff,0);
			graph.beginGradientFill(GradientType.LINEAR,colors,alphs,ratios,matrix);
			graph.drawRoundRectComplex(0,0,width,10,3,3,3,3);
			graph.endFill();
		}
		
		public function resetHP(hp:int):void{
			if(hp < 1){
				hpShape.graphics.clear();
			}else{
				if(hp > 500)
					hp = 500;
				var wid:int = hp / 500 * 100;
				drawHPBar(hpShape.graphics,wid);
			}
		}

		public function setNameText(name : String) : void {
			nameText.autoSize = TextFieldAutoSize.LEFT;
            nameText.background = false;
            nameText.border = false;
            nameText.selectable = false;
			
			var format:TextFormat = new TextFormat();
            format.font = "Verdana";
            format.color = 0xFFFFFF;
            format.size = 12;
            format.underline = false;
            nameText.defaultTextFormat = format;
            
            nameText.text = name;
            TextFilterUtil.textGlow(nameText);
            
            nameText.x = this.width / 2 - nameText.width / 2;
		}
		
		/**
		 * 播放伤害效果
		 * */
		public function playHurtEffect(damage:int):void{
			var damageStr:String = Math.abs(damage).toString();
			var leng:uint = damageStr.length;
			var mergeFig:Sprite = new Sprite();
			while(effectContainer.numChildren > 0)
			{
				effectContainer.removeChildAt(0);
			}
			for(var i:uint = 0;i < leng;i++){
				var figmc:MovieClip = PlatformDepot.getInstance().getMovieClipByName("Artfig"+damageStr.charAt(i));
				mergeFig.addChild(figmc);
				figmc.x = mergeFig.width - 10;
			}
			
			mergeFig.x = Math.round((charactorContainer.width - mergeFig.width) / 2);
			mergeFig.y = -10;
			effectContainer.addChild(mergeFig);
		}
		
		/**
		 * 播放回复效果
		 * */
		public function playResumeEffect(resume:int):void{
			var resumeStr:String = Math.abs(resume).toString();
			var leng:uint = resumeStr.length;
			var mergeFig:Sprite = new Sprite();
			while(effectContainer.numChildren > 0)
			{
				effectContainer.removeChildAt(0);
			}
			var tempmc:MovieClip = PlatformDepot.getInstance().getMovieClipByName("ResumeSign");
			mergeFig.addChild(tempmc);
			for(var i:uint = 0;i < leng;i++){
				var figmc:MovieClip = PlatformDepot.getInstance().getMovieClipByName("Resume"+resumeStr.charAt(i));
				figmc.x = mergeFig.width - 10;
				mergeFig.addChild(figmc);
			}
			
			mergeFig.x = 12;
			mergeFig.y = -10;
			effectContainer.addChild(mergeFig);
		}

		public function initListener() : void {

		}
		
		/**释放*/
		public function dispose() : void {	
			hpShape.graphics.clear();
			DisplayObjectUtil.removeAll(charactorContainer);
			DisplayObjectUtil.removeAll(specialContainer);
			DisplayObjectUtil.removeAll(weaponContainer);
			DisplayObjectUtil.removeAll(effectContainer);
			DisplayObjectUtil.removeAll(this);	
			plateArray = null;
//			if(plateArray!=null)
//				while(plateArray.length > 0){
//					var arr1 : Array = plateArray.shift() as Array;
//					while(arr1!=null&&arr1.length > 0){
//						var arr2 : Array = arr1.shift() as Array;
//						while(arr2!=null&&arr2.length > 0){
//							var data : Bitmap = arr2.shift() as Bitmap;
//							data.bitmapData.dispose();
//							data.bitmapData = null;
//							data = null;							
//						}
//					}					
//				}
		}

	}
	
}